﻿using System;
using CoProjectModel;
using IdeaBlade.Application.Framework.Core.Composition;
using IdeaBlade.Application.Framework.Core.Persistence;

namespace Caliburn.Micro.Logging.IdeaBladeServerLogger
{
    public class IdeaBladeServerLogger : ILog, ILogExtended
    {
        #region Fields
        private readonly object _innerLogger;


        public IEntityManagerProvider<CoProjectEntities> EntityManagerProvider { get; set; }
        private CompositionContext Context { get { return EntityManagerProvider.Context; } }
        private CoProjectEntities Manager { get { return EntityManagerProvider.Manager; } }

        #endregion

        #region Constructors

        public IdeaBladeServerLogger()
        {
            //_innerLogger = nlog.LogManager.GetLogger(type.Name);
        }

        public IdeaBladeServerLogger(Type type)
        {
            //_innerLogger = nlog.LogManager.GetLogger(type.Name);
        }

        #endregion

        #region ILog Members
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        public void Error(Exception exception)
        {
            //_innerLogger.ErrorException(exception.Message, exception);
            LogToServer("ERROR: " + exception.Message);
        }
        /// <summary>
        /// Logs the message as info.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Info(string format, params object[] args)
        {
            //_innerLogger.Info(format, args);
            var str = "Info: " + String.Format(format, args);
            LogToServer(str);
        }
        /// <summary>
        /// Logs the message as a warning.
        /// </summary>
        /// <param name="format">A formatted message.</param><param name="args">Parameters to be injected into the formatted message.</param>
        public void Warn(string format, params object[] args)
        {
            //_innerLogger.Warn(format, args);
            var str = "Warn: " + String.Format(format, args);
            LogToServer(str);
        }
        #endregion
        #region Implementation of ILogExtended
        /// <summary>
        /// Logs the message as error.
        /// </summary>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(string format, params object[] args)
        {
            //_innerLogger.Error(format, args);
            var str = "Error: " + String.Format(format, args);
            LogToServer(str);
        }
        /// <summary>
        /// Logs the exception.
        /// </summary>
        /// <param name="exception">The exception.</param>
        /// <param name="format">A formatted message.</param>
        /// <param name="args">Parameters to be injected into the formatted message.</param>
        public void Error(Exception exception, string format, params object[] args)
        {
            //_innerLogger.ErrorException(string.Format(format, args), exception);
            var str = "Error: " + exception.Message + " | " + String.Format(format, args);
            LogToServer(str);
        }
        #endregion

        private void LogToServer(string message)
        {
            //return;
            var edm = Caliburn.Micro.IoC.Get<IEntityManagerProvider<CoProjectEntities>>();
            var io = edm.Manager.InvokeServerMethodAsync(
                                  "CustomLogging.ServerLogger,CoProjectModel", // Assembly-qualified name for the server class
                                  "Log", // name of the server method
                                  (e) =>
                                  {
                                      var a = e.CompletedSuccessfully;

                                  },
                                  null, // state object to identify this particular call (we don't care)
                                  message);
        }
    }
}
